package com.smt.modules.bus.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.R;
import com.smt.modules.bus.dao.EventManageDao;
import com.smt.modules.bus.entity.EventManagementEntity;
import com.smt.modules.bus.service.EventService;
import com.smt.modules.bus.vo.EventManagementVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 事件管理实现
 *
 * @author zhangyi
 * @date 2018/10/10 17:19
 */
@Service
public class EventServiceImpl extends ServiceImpl<EventManageDao,EventManagementEntity> implements EventService {

    /**
     * 添加事件
     *
     * @param eventManagementVo
     * @return
     */
    @Override
    public R add(EventManagementVo eventManagementVo,Long userId) {
        List<EventManagementEntity> events
                = selectList(new EntityWrapper<>());
        EventManagementEntity eventManagementEntity = new EventManagementEntity();
        boolean[] editFlag = new boolean[]{false};

        events.stream()
                .forEach(e -> {
                    if(e.getProjectId().equals(eventManagementVo.getProjectId())){
                        if(e.getTitle().equals(eventManagementVo.getTitle())){
                            editFlag[0] = true;
                        }
                    }
                });
            if(editFlag[0]){
                return R.error(EnumToKeyUtil.toInt(Code.MODIFY_DATA_ERROR),"此事件已经存在，不能重复添加");
            }else{
                BeanUtils.copyProperties(eventManagementVo,eventManagementEntity);
                eventManagementEntity.setCreateTime(DateTime.now().toDate());
                eventManagementEntity.setMemberId(userId);
                boolean insert = insert(eventManagementEntity);
                return insert?R.ok("添加事件成功"):
                        R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR),"添加事件失败");
            }
    }
}
